#!/bin/bash
#
# chkconfig: 2345 10 10
# description: Door Security System
#
# Copyright 2008 Dan Fraser
#
# This file is part of Cerberus-Prox.
#
# Cerberus-Prox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#  Cerberus-Prox is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with Cerberus-Prox.  If not, see <http://www.gnu.org/licenses/>.
# 
#
# Source function library
. /etc/rc.d/init.d/functions


APP_NAME=doorsystem
APP_HOME=/usr/local/doorsystem
MAC=`ifconfig eth0 | grep HWaddr | awk '{ print $5 }' | sed 's/://g'`
JAVA_HOME=/usr/local/java
PATH=$PATH:$JAVA_HOME/bin
APP_MAIN=com.onestopmediagroup.doorsecurity.Main
RETVAL=0
APP_JAR=doortest.jar

start() {
        echo -n $"Starting ${APP_NAME}: "
	for i in `ls ${APP_HOME}/lib/*.jar`; do
        		APP_CLASSPATH=${APP_CLASSPATH}:${i}
	done
	APP_CLASSPATH=${APP_CLASSPATH}:${APP_JAR}
	cd ${APP_HOME}
	${APP_HOME}/bin/jsvc -wait 20 -classpath "${APP_CLASSPATH}" -Dmac=${MAC} ${APP_MAIN}
 	RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
    	echo_success
    	touch /var/lock/subsys/doorsystem
    else 
    	echo_failure
    fi
	echo
    return $RETVAL
}

stop() {
	echo -n $"Stopping ${APP_NAME}: "
	${APP_HOME}/bin/jsvc -stop ${APP_MAIN}
 	RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
    	echo_success
    else 
    	echo_failure
    fi
	echo
	return $RETVAL
}

 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status servicename
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
  
exit 0
